<?php

namespace app\models;

use Yii;
use app\models\CourseMember;

/**
 * This is the model class for table "gky_course".
 *
 * @property string $id
 * @property string $coursename
 * @property string $courseno
 * @property integer $creadit
 * @property string $grade
 * @property integer $allperiod
 * @property string $profession
 * @property string $classtype1
 * @property string $classtype2
 * @property string $classes
 * @property string $checktype
 * @property integer $studentnum
 * @property integer $teachperiod
 * @property integer $testperiod
 * @property integer $machineperiod
 * @property integer $otherperiod
 * @property integer $weekperiod
 * @property string $teachweek
 * @property string $examineweek
 * @property string $teacher
 * @property string $siterequirement
 * @property string $arrangerequirement
 * @property string $mark
 * @property string $updatetime
 * @property integer $isfinish
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 * @property integer $mon
 * @property integer $tue
 * @property integer $wed
 * @property integer $thu
 * @property integer $fri
 * @property string $regmatch
 * @property string $term
 * @property members[] $member
 */
class Course extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_course';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['creadit', 'allperiod', 'studentnum', 'teachperiod', 'testperiod', 'machineperiod', 'otherperiod', 'weekperiod', 'updatetime', 'isfinish', 'addmanid', 'addtime', 'mon', 'tue', 'wed', 'thu', 'fri'], 'integer'],
            [['regmatch'], 'string'],
            [['coursename', 'classes', 'teacher'], 'string', 'max' => 255],
            [['courseno'], 'string', 'max' => 128],
            [['grade'], 'string', 'max' => 32],
            [['profession', 'classtype1', 'classtype2', 'checktype', 'teachweek', 'examineweek', 'siterequirement', 'arrangerequirement', 'mark', 'addman'], 'string', 'max' => 64],
            [['term'], 'string', 'max' => 126],
            //[['coursename','courseno','grade','allperiod','profession','classes','teachperiod','term', 'checktype','teacher'],'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'coursename' => '课程名称',
            'courseno' => '课程编号',
            'creadit' => '学分',
            'grade' => '年级',
            'allperiod' => '总学时',
            'profession' => '专业',
            'classtype1' => '课程类别-专业课',
            'classtype2' => '课程类别-任选课',
            'classes' => '上课班级',
            'checktype' => '考核方式',
            'studentnum' => '学生人数',
            'teachperiod' => '讲授学时',
            'testperiod' => '实验学时',
            'machineperiod' => '周学时',
            'otherperiod' => '其他学时',
            'weekperiod' => '周学时',
            'teachweek' => '上课周次',
            'examineweek' => '考试周次',
            'teacher' => '任课老师',
            'siterequirement' => '教学场地要求',
            'arrangerequirement' => '排课要求',
            'mark' => '备注',
            'updatetime' => '更新时间',
            'isfinish' => '是否完成',
            'addman' => '记录添加人',
            'addmanid' => '记录添加人编号',
            'addtime' => '记录添加时间',
            'mon' => '星期一',
            'tue' => '星期二',
            'wed' => '星期三',
            'thu' => '星期四',
            'fri' => '星期五',
            'regmatch' => 'Regmatch',
            'term' => '学期',
        ];
    }

    /**
     * 获取本课程的老师
     */
    public function getMembers()
    {
        return $this->hasMany(CourseMember::className(), ['course_id' => 'id']);
    }

    //在这里将 本项目的所有成员保存到project表中
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            // Place your custom code here
            $membersId = Yii::$app->request->post('teachermember');
            if ($membersId != '') {
                for ($i = 0; $i < count($membersId); $i++) {
                    $members[$i] = GkyUser::find()->where(['id' => $membersId[$i]])->One()->username;
                }
                $this->teacher = implode(',', $members);
            }
            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
            $id = Yii::$app->user->identity->id;
            //添加人的id
            $this->addmanid = $id;
            //添加人的姓名
            $name = GkyUser::find()->where(['id' => $id])->one()->username;
            $this->addman = $name;
            //更新时间
            $time = date("Y-m-d");
            $this->updatetime = $time;
            //添加时间
            $this->addtime = $time;
            return true;
        } else {
            return false;
        }
    }

    public static function getList($pageNow, $pageSize){

        $list = Course::find()
            ->select(['id','coursename','teacher','studentnum','classtype2'])
            ->orderBy(['addtime' => SORT_DESC])
            ->offset(($pageNow - 1) * $pageSize)
            ->limit($pageSize)
            ->all();
        return $list;
    }

}
